package cn.wolfcode.Controller;


import cn.wolfcode.domain.MessageBoard;
import cn.wolfcode.service.IMessageBoardService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.jsonResult;
import cn.wolfcode.util.RequirePermission;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/messageBoard")
public class MessageBoardController {

    @Autowired
    private IMessageBoardService messageBoardService;


    @RequirePermission(name = "留言板页面",experssion = "messageBoard:indexMessage")
    @RequestMapping("/indexMessage")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<MessageBoard> pageInfo = messageBoardService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "indexMessage";
    }

    @RequirePermission(name = "留言板页面查看详情",experssion = "messageBoard:massageItem")
    @RequestMapping("/massageItem")
    public String massageItem(Model model, @ModelAttribute("qo") QueryObject qo,Long id){

        List<MessageBoard> messageBoards = messageBoardService.selectForItem(id);
        model.addAttribute("messageBoards", messageBoards);
        return "message/massageItem";
    }

    @RequestMapping("/delete")
@RequirePermission(name = "留言板页面",experssion = "messageBoard:delete")
    @ResponseBody
    public jsonResult delete(Long id){
        if (id != null) {
            messageBoardService.delete(id);
        }
        return new jsonResult();
    }


    @RequestMapping("/saveOrUpdate")
@RequirePermission(name = "留言板页面",experssion = "messageBoard:saveOrUpdate")

    public String saveOrUpdate(MessageBoard messageBoard){
        if (messageBoard.getId() != null) {
            messageBoardService.update(messageBoard);
        }else {
            messageBoardService.save(messageBoard);
        }
        return "redirect:indexMessage";
    }
}
